/**
 * 
 */
package com.cqan.account.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.cqan.common.Constant;
import com.cqan.entity.pk.LongEntity;
import com.cqan.util.ConvertUtils;

/**
 * @Description 用户实体
 * @author slaton.wu@gmail.com
 * @date 2012-6-29下午6:44:23
 * 
 */
@Entity
// 表名与类名不相同时重新定义表名.
@Table(name = "TBL_USER")
// 默认的缓存策略.
@SuppressWarnings("serial")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends LongEntity implements UserDetails {

	private String address;

	/**
	 * 权限集合
	 */
	private transient List<GrantedAuthority> authorities;

	private Date createTime;

	private String email;

	private boolean enabled;

	private Date lastLoginTime;

	private String lastLoginIp;

	private String nickname;

	private String password;

	private String phone;

	private String realName;

	private String regIp;

	private List<Role> roles;

	private String username;

	/**
	 * 性别
	 */
	private String sex;

	/**
	 * 用户级别，0:未注册用户，1:注册用户，2:绑写手机用户
	 */
	private Integer userGrade;

	/**
	 * 用户头像
	 */
	private String avatar;

	/**
	 * 是否为管理员
	 */
	private boolean special;

	// 生日
	private Date birthday;

	// 学历
	private String degree;
	
	/**
	 * 设备ID
	 */
	private String did;
	
	
	@Column(name="DID")
	public String getDid() {
		return did;
	}

	public void setDid(String did) {
		this.did = did;
	}

	@Column(name = "BIRTHDAY")
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Column(name = "DEGREE")
	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

	@Column(name = "ADDRESS")
	public String getAddress() {
		return address;
	}

	@Override
	@Transient
	public List<GrantedAuthority> getAuthorities() {

		return this.authorities;
	}

	@Column(name = "avatar")
	public String getAvatar() {
		if (avatar == null || "".equals(avatar)) {
			return Constant.DEFAULT_AVATAR;
		}
		return avatar;
	}

	@Column(name = "CREATE_TIME")
	public Date getCreateTime() {
		return createTime;
	}

	@Column(name = "EMAIL")
	public String getEmail() {
		return email;
	}

	@Column(name = "LAST_LOGIN_IP")
	public String getLastLoginIp() {
		return lastLoginIp;
	}

	@Column(name = "LAST_LOGIN_TIME")
	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	@Column(name = "NICKNAME")
	public String getNickname() {
		return nickname;
	}

	@JsonIgnore
	@Column(name = "PASSWORD")
	public String getPassword() {
		return password;
	}

	@Column(name = "PHONE")
	public String getPhone() {
		return phone;
	}

	@Column(name = "REAL_NAME")
	public String getRealName() {
		return realName;
	}

	@Column(name = "REGIP")
	public String getRegIp() {
		return regIp;
	}

	@JsonIgnore
	// 多对多定义
	@ManyToMany
	// 中间表定义,表名采用默认命名规则
	@JoinTable(name = "TBL_USER_ROLE", joinColumns = { @JoinColumn(name = "UID") }, inverseJoinColumns = { @JoinColumn(name = "RID") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoles() {
		return roles;
	}

	public String getSex() {
		return sex;
	}

	@Column(name = "USER_GRADE")
	public Integer getUserGrade() {
		return userGrade;
	}

	@Override
	@Column(name = "USER_NAME")
	public String getUsername() {

		return username;
	}

	@Override
	@Transient
	public boolean isAccountNonExpired() {

		return true;
	}

	@Override
	@Transient
	public boolean isAccountNonLocked() {

		return enabled;
	}

	@Override
	@Transient
	public boolean isCredentialsNonExpired() {

		return true;
	}

	@Column(name = "ENABLED")
	public boolean isEnabled() {
		return enabled;
	}

	@Column(name = "SPECIAL")
	public boolean isSpecial() {
		return special;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setAuthorities(List<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public void setRegIp(String regIp) {
		this.regIp = regIp;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public void setSpecial(boolean special) {
		this.special = special;
	}

	public void setUserGrade(Integer userGrade) {
		this.userGrade = userGrade;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * 用户拥有的角色id字符串, 多个角色id用','分隔.
	 */
	// 非持久化属性.
	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getRoleIds() {
		return ConvertUtils.convertElementPropertyToList(roles, "id");
	}

	@Override
	public String toString() {

		return ToStringBuilder.reflectionToString(this);
	}

}
